/*
 * @(#)CheckboxTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import javax.servlet.jsp.JspException;

import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

/**
  CheckboxTag.java

  <p>This class is to to addin javascript checing during input checkbox onchange. </p>
  <p>Purpose to Override struts taglib for tracing form modification.<p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;org.apache.struts.taglib.html.CheckboxTag&lt;/tagclass&gt;
     [TO]
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.CheckboxTag&lt;/tagclass&gt;

     [ADDED]
     &lt;attribute&gt;
     &lt;name&gt;textOnly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
  </pre>
  </p>

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   09/07/2003
    @version         $Revision: 1.10 $
*/

public class CheckboxTag extends org.apache.struts.taglib.html.CheckboxTag {

  public static final String REVISION = "$Revision: 1.10 $";

  protected boolean textOnly = false;

  public int enhancedDoStartTag() throws JspException {
    // Create an appropriate "input" element based on our parameters
    StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
    results.append(" name=\"");
    // * @since Struts 1.1
    if( indexed )
      prepareIndex( results, name );
    results.append(this.property);
    results.append("\"");
    if (accesskey != null) {
      results.append(" accesskey=\"");
      results.append(accesskey);
      results.append("\"");
    }
    if (tabindex != null) {
      results.append(" tabindex=\"");
      results.append(tabindex);
      results.append("\"");
    }
    results.append(" value=\"");
    if (value == null) {
      results.append("on");
    } else {
      results.append(value);
    }
    results.append("\"");
    Object result = RequestUtils.lookup(pageContext, name,
                                          property, null);
    if (result == null) {
      result = "";
    }
    if (!(result instanceof String)) {
      if (result instanceof String[]) {
        boolean matchFlag = false;
        for (int i = 0; i < ((String[])result).length; i++) {
          if (((String[])result)[i].equalsIgnoreCase(value)) {
            result = ((String[])result)[i];
            matchFlag = true;
            break;
          }
        }
        if (!matchFlag) {
          result = result.toString();
        }
      } else {
        result = result.toString();
      }
    }
    String checked = (String) result;
    if (checked.equalsIgnoreCase(value)
        || checked.equalsIgnoreCase("true")
        || checked.equalsIgnoreCase("yes")
        || checked.equalsIgnoreCase("on"))
      results.append(" checked=\"checked\"");
    results.append(prepareEventHandlers());
    results.append(prepareStyles());
    results.append(getElementClose());

    // Print this field to our output writer
    ResponseUtils.write(pageContext, results.toString());

    // Continue processing this page
    this.text = null;
    return (EVAL_BODY_BUFFERED);
  }

  public int doStartTag() throws JspException {
    switch (BaseHandler.commonRoutine(this, pageContext, this.getTextOnly())) {
      case BaseHandler.DIRECT_SKIP_BODY:
        return SKIP_BODY;
      case BaseHandler.DIRECT_EVAL_BODY_BUFFERED:
        return EVAL_BODY_BUFFERED;
      case BaseHandler.RETURN_SUPER_DOSTART:
        return this.enhancedDoStartTag();
      default:
        return this.enhancedDoStartTag();
    }
  }

  public int doEndTag() throws JspException {
    int result = super.doEndTag();
    this.release();
    return(result);
  }

  public void setTextOnly(boolean val){
    textOnly = val;
  }

  public boolean getTextOnly(){
    return textOnly;
  }

  public void release() {
    super.release();
    this.textOnly = false;
    this.setOnclick(null);
    this.setOnchange(null);
    this.setOnselect(null);
  }
}
